//
//  RhombusView.m
//  yuikemall
//
//  Created by Gang.Wang on 12-6-27.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "RhombusView.h"
#import "header.h"

@interface RhombusView()
{
    CGContextRef mCtx;
    
    CGPoint point1_;
    CGPoint point2_;
    
    CGPoint point3_;
    CGPoint point4_;
    
}

@property (nonatomic, assign) CGPoint point1;
@property (nonatomic, assign) CGPoint point2;

@property (nonatomic, assign) CGPoint point3;
@property (nonatomic, assign) CGPoint point4;



- (void) caclePoints;
- (void) privateInit;

@end


@implementation RhombusView

@synthesize point1 = point1_;
@synthesize point2 = point2_;

@synthesize point3 = point3_;
@synthesize point4 = point4_;



- (void) dealloc
{
    [super dealloc];
}



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        [self privateInit];
    }
    
    return self;
}



#pragma mark - Private Method
- (void) privateInit
{
    if (self.bInited) 
    {
        return;
    }
    
    [self setBackgroundColor:COLOR_CLEAR];
    
    
    [self reAdjustLayout];
    mCtx = UIGraphicsGetCurrentContext();  
    self.bInited = TRUE;      
}


- (void) caclePoints
{
    self.point1 = CGPointMake(self.width / 2, 0);
    self.point2 = CGPointMake(0, self.height / 2);
    
    self.point3 = CGPointMake(self.width / 2, self.height);
    self.point4 = CGPointMake(self.width, self.height / 2);
    
    [self setNeedsDisplay];
}


- (BOOL) isVisibleAtPoint:(CGPoint)point
{
    BOOL result = [YMath pointIsInPolygonArea:point, 
                   self.point1,
                   self.point2,
                   self.point3,
                   self.point4,
                   CGPOINT_INVALID];
    
    DebugLog(@"%d  %@", result, self.description);
    return result;
}

#pragma mark - Public Method
- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    [self caclePoints];
}


- (void) drawRhombus
{
    UIBezierPath* path = [UIBezierPath bezierPath];   
    [path moveToPoint:self.point1];
    [path addLineToPoint:self.point2];
    [path addLineToPoint:self.point3];
    [path addLineToPoint:self.point4];
    [path closePath];   
    [path addClip];
    CGContextAddPath(mCtx, path.CGPath);
    
    CGContextClip(mCtx);
    
    [image drawInRect:self.bounds];
    
    CGContextRestoreGState(mCtx);  
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    if (!mCtx) {
        mCtx = UIGraphicsGetCurrentContext();  
    }
    
    CGContextSaveGState(mCtx);
    [self drawRhombus];
}


@end
